﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crystal.CLL;
using CrystalCommon.Presenter;
using Crystal.Common.Model;

namespace CrystalCommon.View
{
    public partial class AddValue : BaseForm, IAddValueView
    {

        #region Fields
        private byte intParam = 0;
        public int lintPublicDataSource = 3;
        public string ID = string.Empty;
        public string mstrAddedValue = string.Empty;
        private IAddValuePresenter AddValuePresenter;
        public event AddEventHandler Add;
        public event GetTableNameEvent GetTableName;
        public EntityType entitytype = new EntityType();
        #endregion

        #region Properties
        public EntityType CurrentType
        {
            get { return entitytype; }
            set
            {
                entitytype = value;
            }
        }
        public byte ParameterInt
        {
            get { return intParam; }
            set
            {
                intParam = value;
            }
        }

        #endregion

        public AddValue(string type, byte sourceId, string Field)
        //public AddValue(string type)
        {
            InitializeComponent();
            SetProperties(type, sourceId, Field);
            //entitytype.Parameter = ParameterInt;
            AddValuePresenter = new AddValuePresenter(this);
        }

        private void SetProperties(string type, byte sourceId, string Field)
        {
            this.Text = MessageManager.GetMessage(DBResult.ADDSOURCE.ToString(), Field).ToString();
            gbContainer.Text = this.Text;
            lblLabel.Text = Field;
            entitytype.SourceId = sourceId;
            entitytype.Name = type;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string[] larrDataSources = new string[] { "factset", "factsets", "lionshare", "lionshares", "public", "publics" };
            string returnID = string.Empty;
            CurrentType.Parameter = ParameterInt;
            if (txtAdd.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Please enter " + lblLabel.Text, "Crystal", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (larrDataSources.Contains(txtAdd.Text.ToLower().Trim()))
            {
                MessageBox.Show(MessageManager.GetMessage("ADD_VALUE_DIFFERNAME"), "Crystal", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            Add(txtAdd.Text.Trim(), CurrentType, ref returnID);
            ID = returnID;
            mstrAddedValue = txtAdd.Text.Trim();
            this.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddValue_Load(object sender, EventArgs e)
        {
            ChangeButtonText();
            txtAdd.Focus();
        }

        private void ChangeButtonText()
        {
            if (CurrentType.SourceId == Convert.ToByte(LKPMaster.EDSDSN))
            {
                btnSave.Text = "&OK";
            }
        }

    }
}
